package com.tanhua.management.controller;

import com.tanhua.management.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

@RestController
@RequestMapping("/users")
public class Login {
    @Autowired
    private UserService userService;

    @GetMapping(value = "/captcha", name = "获取验证码")
    public ResponseEntity verificationCode(String uuid, HttpServletResponse response) throws IOException {
        userService.getCaptchaCode(uuid, response);
        return ResponseEntity.ok(null);
    }

    @PostMapping(value = "/login", name = "后台用户登录")
    public ResponseEntity login(@RequestBody Map<String,String> map){
        String username = map.get("name");
        String password = map.get("pwd");
        String code = map.get("cd");
        String uuid = map.get("uid");

        String token = userService.login(username, password, code, uuid);
        if(token == null){
            return ResponseEntity.status(400).body("0");
        }
        return ResponseEntity.ok(token);
    }
}
